package cn.edu.nju.software.greencreditsystem.business;

import cn.edu.nju.software.greencreditsystem.DAOImpl.FinancialStandardValueDAOImpl;
import cn.edu.nju.software.greencreditsystem.util.DataReader;

/**
 * 作者：何启睿
 * 创建时间：2011-8-2
 * 类描述：财务分析工具类（实现）
 */
public class WoleAnalyser implements FinancialAnalyser
{
    public static final int WC = 0;//营运资金,working capital
    public static final int CR = 1;//流动比率,current ratio
    public static final int QR = 2;//速动比率,quick ratio
    public static final int CAR = 3;//现金比率,cash asset ratio
    public static final int DAR = 4;//资产负债率,debt asset ratio
    public static final int ER = 5;//产权比率,equity ratio
    public static final int ICR = 6;//利息保障倍数,interest coverage ratio
    public static final int OM = 7;//营业利润率,operating margin
    public static final int GPM = 8;//毛利率,gross profit margin
    public static final int SM = 9;//销售净利率,sales margin
    public static final int ROA = 10;//资产收益率,return on asset
    public static final int TAT = 11;//总资产周转率,total asset turnover
    public static final int FAT = 12;//固定资产周转率,fixed asset turnover
    public static final int CAT = 13;//流动资产周转率,current asset turnover
    public static final int ART = 14;//应收账款周转率,accoun receivable turnover
    public static final int IT = 15;//存货周转率,inventory turnover
    public static final int TAG = 16;//总资产增长率,total asset growth
    public static final int RG = 17;//营业收入增长率,revenue growth
    public static final int NPG = 18;//净利润增长率,net profit growth
    public static final int FAR = 19;//固定资产比率,Fixed assets ratio
    public static final int NAT = 20;//净资产周转率,Net asset turnover
    public static final int WS = 21;//沃尔评分,wole score
    
    public static final String factorNames[] = 
        {"营运资金","流动比率","速动比率","现金比率",
        "资产负债率","产权比率","利息保障倍数","营业利润率",
        "毛利率","销售净利率","资产收益率","总资产周转率",
        "固定资产周转率","流动资产周转率","应收账款周转率","存货周转率",
        "总资产增长率","营业收入增长率","净利润增长率","固定资产比率",
        "净资产周转率","沃尔评分"};
    
    @Override
    public double[] getIndex(String xmlPath, String excelPath) throws Exception
    {
        double result[] = new double[factorNames.length];
        
        String dataToRead[] = {"期末流动资产", "期末流动负债", "期末存货", "期末预付账款"
                                , "期末待摊费用", "期末货币资金", "期末交易性金融资产", "期末负债"
                                , "期末资产", "期末所有者权益", "本期利润总额", "本期财务费用"
                                , "本期营业利润", "本期营业收入", "本期营业成本", "本期净利润"
                                , "期初资产", "期末固定资产", "期初固定资产", "期初流动资产"
                                , "期末应收账款", "期初应收账款", "期初存货", "上期营业收入"
                                , "上期净利润", "期初所有者权益"};//18 from 资产负债表；8 from 利润表
        DataReader reader = new DataReader(xmlPath, excelPath, dataToRead);
        double rawData[] = reader.readData();
        double standardValue[] = new FinancialStandardValueDAOImpl().getStandardValues();
        
        result[WC] = rawData[0] - rawData[1];
        result[CR] = rawData[0] / rawData[1];
        result[QR] = (rawData[0] - rawData[2] - rawData[3] - rawData[4]) / rawData[1];
        result[CAR] = (rawData[5] + rawData[6]) / rawData[1];
        result[DAR] = rawData[7] / rawData[8];
        result[ER] = rawData[7] / rawData[9];
        result[ICR] = rawData[10] / rawData[11];
        result[OM] = rawData[12] / rawData[13];
        result[GPM] = (rawData[13] - rawData[14]) / rawData[13];
        result[SM] = rawData[15] / rawData[13];
        result[ROA] = rawData[15] / ((rawData[8] + rawData[16])/2);
        result[TAT] = rawData[13] / ((rawData[8] + rawData[16])/2);
        result[FAT] = rawData[13] / ((rawData[17] + rawData[18])/2);
        result[CAT] = rawData[13] / ((rawData[0] + rawData[19])/2);
        result[ART] = rawData[13] / ((rawData[20] + rawData[21])/2);
        result[IT] = rawData[14] / ((rawData[2] + rawData[22])/2);
        result[TAG] = (rawData[8] - rawData[16]) / rawData[16];
        result[RG] = (rawData[13] - rawData[23]) / rawData[23];
        result[NPG] = (rawData[15] - rawData[24]) / rawData[24];
        result[FAR] = rawData[17] / rawData[8];
        result[NAT] = rawData[13] / ((rawData[9] + rawData[25])/2);
        result[WS] = result[CR] / standardValue[CR] * 25 +
                     result[DAR] / standardValue[DAR] * 25 +
                     result[FAR] / standardValue[FAR] * 15 +
                     result[IT] / standardValue[IT] * 10 +
                     result[ART] / standardValue[ART] * 10 +
                     result[FAT] / standardValue[FAT] * 10 +
                     result[NAT] / standardValue[NAT] * 5;
        
        return result;
    }
}
